package com.corpgovernment.organization.service.budget;

import com.corpgovernment.api.car.product.dto.request.QueryOrderDetailRequestVo;
import com.corpgovernment.api.car.product.dto.response.QueryOrderDetailResponseVo;
import com.corpgovernment.api.flight.product.vo.orderdetail.enums.OrderStatusEnum;
import com.corpgovernment.api.hotel.product.vo.CheckHotelOrderAmountRequest;
import com.corpgovernment.api.hotel.product.vo.QueryHotelOrderDetailResponseVo;
import com.corpgovernment.api.messageadvice.vo.EmailInfo;
import com.corpgovernment.api.organization.dto.request.CheckAllOrderAmountRequest;
import com.corpgovernment.api.platform.soa.response.GetMbBudgetInfoResponse;
import com.corpgovernment.api.platform.soa.response.GetOrderBillDateResponse;
import com.corpgovernment.common.base.AbstractBaseService;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.dto.AllOrderBudgetResponseDto;
import com.corpgovernment.common.enums.OrderTypeEnum;
import com.corpgovernment.common.utils.DateUtil;
import com.corpgovernment.common.utils.ListUtils;
import com.corpgovernment.organization.dataloader.*;
import com.corpgovernment.organization.dataloader.db.HotelOrderClientLoader;
import com.corpgovernment.organization.entity.db.MbOrgInfo;
import com.corpgovernment.organization.entity.db.budget.BudgetWarningEntity;
import com.corpgovernment.organization.mapper.MbOrgInfoMapper;
import com.corpgovernment.organization.mapper.budget.BudgetWarningMapper;
import com.corpgovernment.organization.mq.SendEmailMQProducer;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BudgetCountServicev1 extends AbstractBaseService {

    @Autowired
    private PpPaymentBillClientLoader ppPaymentBillClientLoaderl;
    @Autowired
    private CarOrderClientLoader carOrderClientLoader;
    @Autowired
    private HotelOrderClientLoader hotelOrderClientLoader;
    @Autowired
    private AddBudgetService addBudgetService;
    @Autowired
    private SearchBudgetService searchBudgetService;
    @Autowired
    private FlightClientLoader flightClientLoader;
    @Autowired
    private HotelLintlOrderClientLoader hotelLintlOrderClientLoader;
    @Autowired
    private IntlFlightClientLoader intlFlightClientLoader;
    @Autowired
    private TrainOrderClientLoader trainOrderClientLoader;
    @Autowired
    private MbOrgInfoMapper mbOrgInfoMapper;

    @Autowired
    private BudgetWarningMapper budgetWarningMapper;
    @Autowired
    private SendEmailMQProducer sendEmailMQProducer;

    private static final Long Y = 1L;
    private static final Long N = 2L;


    public JSONResult<Object> count(QueryOrderDetailRequestVo request) {
        try {
            initElkLog();
            CheckHotelOrderAmountRequest checkAllOrderAmountRequest = new CheckHotelOrderAmountRequest();
            checkAllOrderAmountRequest.setFlag(false);
            //酒店只计算已成交得单子     则只有入住人已入住完成则会触发扣款   其他场景不会  则只有支付单逻辑  不处理退款单逻辑

            //国内酒店
            hotelOrderDataManage(checkAllOrderAmountRequest);
            //海外酒店
            hotelLintlOrderDataManage(checkAllOrderAmountRequest);
            //打车
            carOrderDataManage(checkAllOrderAmountRequest);
            //国内机票
            flightOrderDataManage(checkAllOrderAmountRequest);
            //国际机票
            flightLintlOrderDataManage(checkAllOrderAmountRequest);
            //火车
            trainOrderDataManage(checkAllOrderAmountRequest);
            return JSONResult.ok();
        } finally {
            log.info("所有产线支付单金额预算处理: {}", getElkInfoLog());
            clearElkLog();
        }
    }

    private void trainOrderDataManage(CheckHotelOrderAmountRequest checkAllOrderAmountRequest) {
        //查询出所有昨日对金额有过处理得订单
        List<GetOrderBillDateResponse> orderBillDataByType = getOrderBillDataByType(OrderTypeEnum.TN.getType());
        addElkInfoLog("开始处理火车票预算逻辑, 需要处理得火车票支付单 %s", JsonUtils.toJsonString(orderBillDataByType));
        List<Long> collect = orderBillDataByType.stream().map(GetOrderBillDateResponse::getOrderId).collect(Collectors.toList());


        //根据订单号查询出所有得订单 然后判断当前订单得状态进行处理
        AllOrderBudgetResponseDto responseDto = trainOrderClientLoader.getTrainPassData(collect);
        Map<String, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> flightPassData = responseDto.getFilght();


        addElkInfoLog("根据支付单id获取到得需要处理订单数据 %s", JsonUtils.toJsonString(flightPassData));
        if (MapUtils.isEmpty(flightPassData)) {
            addElkInfoLog("无火车订单 无需处理");
            return;
        }

        checkFilghtTAOrder(flightPassData, orderBillDataByType.stream().filter(a -> !a.getIsRefund()).collect(Collectors.toList()), "train");


        checkFlightRAOrder(flightPassData, orderBillDataByType, "train");
    }

    private void flightLintlOrderDataManage(CheckHotelOrderAmountRequest checkAllOrderAmountRequest) {
        //查询出所有昨日对金额有过处理得订单
        List<GetOrderBillDateResponse> orderBillDataByType = getOrderBillDataByType(OrderTypeEnum.FI.getType());
        addElkInfoLog("开始处理海外机票预算逻辑, 需要处理得机票支付单 %s", JsonUtils.toJsonString(orderBillDataByType));
        List<Long> collect = orderBillDataByType.stream().map(GetOrderBillDateResponse::getOrderId).collect(Collectors.toList());


        AllOrderBudgetResponseDto responseDto = intlFlightClientLoader.getFlightPassData(collect);
        Map<String, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> flightPassData = responseDto.getFilght();
        //根据订单号查询出所有得订单 然后判断当前订单得状态进行处理


        addElkInfoLog("根据支付单id获取到得需要处理订单数据 %s", JsonUtils.toJsonString(flightPassData));
        if (MapUtils.isEmpty(flightPassData)) {
            addElkInfoLog("无国际酒店订单 无需处理");
            return;
        }


        checkFilghtTAOrder(flightPassData, orderBillDataByType.stream().filter(a -> !a.getIsRefund()).collect(Collectors.toList()), "intlflight");


        checkFlightRAOrder(flightPassData, orderBillDataByType, "intlflight");
    }

    private void hotelLintlOrderDataManage(CheckHotelOrderAmountRequest request) {
        //需要扣款的人

        AllOrderBudgetResponseDto hotelLintlOrder = hotelLintlOrderClientLoader.getHotelLintlOrderAmount();

        addElkInfoLog("开始处理海外酒店预算逻辑 %s", JsonUtils.toJsonString(hotelLintlOrder.getResponses()));
        List<AllOrderBudgetResponseDto.QueryOrderDetailResponse> queryHotelOrderDetailResponseVos = hotelLintlOrder.getResponses();
        if (CollectionUtils.isEmpty(queryHotelOrderDetailResponseVos)) {
            return;
        }

        //查询预定人成本中心
        List<String> corpCollect = queryHotelOrderDetailResponseVos.stream().map(AllOrderBudgetResponseDto.QueryOrderDetailResponse::getCorpId).collect(Collectors.toList());


        List<QueryOrderDetailResponseVo> list = ListUtils.copyList(queryHotelOrderDetailResponseVos, QueryOrderDetailResponseVo.class);

        List<GetOrderBillDateResponse> orderBillData = getOrderBillData(OrderTypeEnum.HI.getType(), list);
        Map<Long, List<GetOrderBillDateResponse>> bill = orderBillData.stream().filter(a -> !Objects.isNull(a.getOrderId())).collect(Collectors.groupingBy(GetOrderBillDateResponse::getOrderId));

        for (QueryOrderDetailResponseVo responseVo : list) {
            List<GetOrderBillDateResponse> responses = bill.get(responseVo);
            if (responseVo.getAmount() == null) {
                responseVo.setAmount(BigDecimal.ZERO);
            }

            for (GetOrderBillDateResponse respons : responses) {
                responseVo.setAmount(responseVo.getAmount().add(respons.getAmount()));
            }
        }

        calculatedAmount1(list, queryCorpIdByOrgId(corpCollect), "intlhotel");
    }

    /**
     * 国内机票计算
     */
    private void flightOrderDataManage(CheckHotelOrderAmountRequest checkAllOrderAmountRequest) {
        //查询出所有昨日对金额有过处理得订单
        List<GetOrderBillDateResponse> orderBillDataByType = getOrderBillDataByType(OrderTypeEnum.FN.getType());
        addElkInfoLog("开始处理国内机票预算逻辑, 需要处理得机票支付单 %s", JsonUtils.toJsonString(orderBillDataByType));
        List<Long> collect = orderBillDataByType.stream().map(GetOrderBillDateResponse::getOrderId).collect(Collectors.toList());


        //根据订单号查询出所有得订单 然后判断当前订单得状态进行处理
        AllOrderBudgetResponseDto responseDto = flightClientLoader.getFlightPassData(collect);
        Map<String, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> flightPassData = responseDto.getFilght();


        addElkInfoLog("根据支付单id获取到得需要处理订单数据 %s", JsonUtils.toJsonString(flightPassData));
        if (MapUtils.isEmpty(flightPassData)) {
            addElkInfoLog("无国内酒店订单 无需处理");
            return;
        }

        checkFilghtTAOrder(flightPassData, orderBillDataByType.stream().filter(a -> !a.getIsRefund()).collect(Collectors.toList()), "flight");


        checkFlightRAOrder(flightPassData, orderBillDataByType, "flight");
    }

    /**
     * 计算国内机票昨日买票数据   如没有则查看改签
     */
    private void checkFlightRAOrder(Map<String, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> flightPassData, List<GetOrderBillDateResponse> getOrderBillDateResponses, String type) {
        addElkInfoLog("退款单支付单数据 %s", JsonUtils.toJsonString(getOrderBillDateResponses));
        List<AllOrderBudgetResponseDto.QueryOrderDetailResponse> queryFlightOrderDetailResponseVos = flightPassData.get(OrderStatusEnum.RA.getType());
        if (CollectionUtils.isEmpty(queryFlightOrderDetailResponseVos)) {
            queryFlightOrderDetailResponseVos = flightPassData.get(OrderStatusEnum.EA.getType());
        }

        if (CollectionUtils.isEmpty(queryFlightOrderDetailResponseVos)) {
            return;
        }
        //  处理支付单数据   当日下单 && 当日退票   计算手续费
        getOrderBillDateResponses = checkBillData(getOrderBillDateResponses);

        queryFlightOrderDetailResponseVos.stream().forEach(a -> {
            a.setAmount(BigDecimal.ZERO);
        });

        Map<Long, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> collect = queryFlightOrderDetailResponseVos.stream().collect(Collectors.groupingBy(AllOrderBudgetResponseDto.QueryOrderDetailResponse::getOrderId));
        List<QueryOrderDetailResponseVo> orderBillData = new ArrayList<>();
        for (GetOrderBillDateResponse getOrderBillDateRespons : getOrderBillDateResponses) {
            List<AllOrderBudgetResponseDto.QueryOrderDetailResponse> responseVos = collect.get(getOrderBillDateRespons.getRefundId());
            if (CollectionUtils.isEmpty(responseVos)) {
                continue;
            }
            BigDecimal divide = getOrderBillDateRespons.getAmount().divide(new BigDecimal(responseVos.size()));
            //判断是否为支付单  出现这种场景的为 1。当日订票  当日改签  或者当日退票

            for (AllOrderBudgetResponseDto.QueryOrderDetailResponse responseVo : responseVos) {
                responseVo.setAmount(responseVo.getAmount().add(divide));
            }

            orderBillData.addAll(ListUtils.copyList(responseVos, QueryOrderDetailResponseVo.class));
        }

        List<String> corpList = queryFlightOrderDetailResponseVos.stream().map(AllOrderBudgetResponseDto.QueryOrderDetailResponse::getOrgId).collect(Collectors.toList());

        addElkInfoLog("处理退款单||改签单数据: %s", JsonUtils.toJsonString(orderBillData));

        calculatedAmount1(orderBillData, queryCorpIdByOrgId(corpList), type);
    }

    private List<GetOrderBillDateResponse> checkBillData(List<GetOrderBillDateResponse> getOrderBillDateResponses) {
        List<GetOrderBillDateResponse> collect = getOrderBillDateResponses.stream().filter(a -> a.getIsRefund()).collect(Collectors.toList());

        Map<Long, List<GetOrderBillDateResponse>> map = getOrderBillDateResponses.stream().filter(a -> !a.getIsRefund()).collect(Collectors.groupingBy(GetOrderBillDateResponse::getOrderId));

        for (GetOrderBillDateResponse response : collect) {
            List<GetOrderBillDateResponse> responses = map.get(response.getOrderId());
            if (CollectionUtils.isEmpty(responses)) {
                response.setAmount(BigDecimal.ZERO.subtract(response.getAmount()));
                continue;
            }

            BigDecimal num = BigDecimal.ZERO;
            for (GetOrderBillDateResponse respons : responses) {
                num = num.add(respons.getAmount());
            }

            response.setAmount(num.subtract(response.getAmount()));
            //只计算一次
            map.remove(response.getOrderId());
        }


        return collect;
    }

    /**
     * 计算国内机票  昨日有退票情况
     */
    private void checkFilghtTAOrder(Map<String, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> flightPassData, List<GetOrderBillDateResponse> getOrderBillDateResponses, String type) {
        List<AllOrderBudgetResponseDto.QueryOrderDetailResponse> queryFlightOrderDetailResponseVos = flightPassData.get(OrderStatusEnum.TA.getType());
        if (CollectionUtils.isEmpty(queryFlightOrderDetailResponseVos)) {
            queryFlightOrderDetailResponseVos = flightPassData.get(OrderStatusEnum.EA.getType());
        }
        if (CollectionUtils.isEmpty(queryFlightOrderDetailResponseVos)) {
            return;
        }
        addElkInfoLog("支付单支付单数据 %s", JsonUtils.toJsonString(getOrderBillDateResponses));
        queryFlightOrderDetailResponseVos.stream().forEach(a -> {
            a.setAmount(BigDecimal.ZERO);
        });

        Map<Long, List<AllOrderBudgetResponseDto.QueryOrderDetailResponse>> collect = queryFlightOrderDetailResponseVos.stream().collect(Collectors.groupingBy(AllOrderBudgetResponseDto.QueryOrderDetailResponse::getOrderId));
        List<QueryOrderDetailResponseVo> orderBillData = new ArrayList<>();
        for (GetOrderBillDateResponse getOrderBillDateRespons : getOrderBillDateResponses) {
            //GetOrderBillDateResponse queryFlightOrderDetailResponse = JSONObject.parseObject(JSONObject.toJSONString(getOrderBillDateRespons), GetOrderBillDateResponse.class);
            List<AllOrderBudgetResponseDto.QueryOrderDetailResponse> responseVos = collect.get(getOrderBillDateRespons.getOrderId());
            if (CollectionUtils.isEmpty(responseVos)) {
                continue;
            }

            BigDecimal divide = getOrderBillDateRespons.getAmount().divide(new BigDecimal(responseVos.size()));

            for (AllOrderBudgetResponseDto.QueryOrderDetailResponse responseVo : responseVos) {
                responseVo.setAmount(responseVo.getAmount().add(divide));
            }
            orderBillData.addAll(ListUtils.copyList(responseVos, QueryOrderDetailResponseVo.class));
        }

        List<String> corpList = queryFlightOrderDetailResponseVos.stream().map(AllOrderBudgetResponseDto.QueryOrderDetailResponse::getOrgId).collect(Collectors.toList());


        calculatedAmount1(orderBillData, queryCorpIdByOrgId(corpList), type);
    }

    private List<QueryOrderDetailResponseVo> hotelOrderDataManage(CheckHotelOrderAmountRequest request) {

        //查询昨日已完成得订单   根据订单查询支付款   再根据酒店得预定人进行预算管控   2022-01-06 00:00:00.000

        //需要扣款的人
        Map<Long, List<QueryHotelOrderDetailResponseVo>> hotelOrderAmount = hotelOrderClientLoader.getHotelOrderAmount(request);
        addElkInfoLog("开始处理酒店预算逻辑 %s", JsonUtils.toJsonString(hotelOrderAmount));
        List<QueryHotelOrderDetailResponseVo> queryHotelOrderDetailResponseVos = hotelOrderAmount.get(Y);
        if (CollectionUtils.isEmpty(queryHotelOrderDetailResponseVos)) {
            return null;
        }

        //查询预定人成本中心
        List<String> corpCollect = queryHotelOrderDetailResponseVos.stream().map(QueryHotelOrderDetailResponseVo::getOrgId).collect(Collectors.toList());


        List<QueryOrderDetailResponseVo> list = ListUtils.copyList(queryHotelOrderDetailResponseVos, QueryOrderDetailResponseVo.class);

        List<GetOrderBillDateResponse> orderBillData = getOrderBillData(OrderTypeEnum.HN.getType(), list);
        Map<Long, List<GetOrderBillDateResponse>> bill = orderBillData.stream().filter(a -> !Objects.isNull(a.getOrderId())).collect(Collectors.groupingBy(GetOrderBillDateResponse::getOrderId));

        for (QueryOrderDetailResponseVo responseVo : list) {
            List<GetOrderBillDateResponse> responses = bill.get(responseVo.getOrderId());
            if (responseVo.getAmount() == null) {
                responseVo.setAmount(BigDecimal.ZERO);
                for (GetOrderBillDateResponse respons : responses) {
                    responseVo.setAmount(responseVo.getAmount().add(respons.getAmount()));
                }
            }
        }

        calculatedAmount1(list, queryCorpIdByOrgId(corpCollect), "hotel");


       /* //查询今日是否有退房单  & 查看付款是否今日付款     如同一日则不处理   如支付单在今日之前则处理金额
        Map<Long, List<GetOrderBillDateResponse>> collect = orderBillData.stream().filter(a -> !Objects.isNull(a.getRefundId())).collect(Collectors.groupingBy(GetOrderBillDateResponse::getRefundId));
        Set<Long> longs = collect.keySet();
        if (CollectionUtils.isEmpty(longs)) {
            return null;
        }
        request.setOrderIds((List<Long>) longs);
        Map<Long, List<QueryHotelOrderDetailResponseVo>> hotelOrder = hotelOrderClientLoader.getHotelOrderAmount(request);
        List<QueryHotelOrderDetailResponseVo> queryHotelOrderDetailResponse = hotelOrder.get(N);
        addElkInfoLog("非今日订房且今日退房数据 %s", JSONObject.toJSONString(hotelOrderAmount));


        //查询预定人成本中心
        corpCollect = queryHotelOrderDetailResponse.stream().map(QueryHotelOrderDetailResponseVo::getOrgId).collect(Collectors.toList());

        list = copyList(queryHotelOrderDetailResponseVos, QueryOrderDetailResponseVo.class);

        orderBillData = getOrderBillData(OrderTypeEnum.HN.getType(), list);
        bill = orderBillData.stream().filter(a -> !Objects.isNull(a.getOrderId())).collect(Collectors.groupingBy(GetOrderBillDateResponse::getOrderId));

        for (QueryOrderDetailResponseVo responseVo : list) {
            List<GetOrderBillDateResponse> responses = bill.get(responseVo.getOrderId());
            if (responseVo.getAmount() == null) {
                responseVo.setAmount(BigDecimal.ZERO);
            }

            for (GetOrderBillDateResponse respons : responses) {
                responseVo.setAmount(responseVo.getAmount().add(respons.getAmount()));
            }
        }

        calculatedAmount1(list, queryCorpIdByOrgId(corpCollect), "hotel");*/


        return null;
    }

    private List<GetOrderBillDateResponse> getOrderBillData(String type, List<QueryOrderDetailResponseVo> list) {
        List<GetOrderBillDateResponse> orderBillData = ppPaymentBillClientLoaderl.getOrderBillData(false,
                list.stream().map(a -> String.valueOf(a.getOrderId())).collect(Collectors.toList()), type, null, null);
        return JsonUtils.parseArray(JsonUtils.toJsonString(orderBillData), GetOrderBillDateResponse.class);
    }

    private List<GetOrderBillDateResponse> getOrderBillDataByType(String type) {
        return ppPaymentBillClientLoaderl.getOrderBillDataByType(false,
                Lists.newArrayList(), type);
    }

    /**
     * 打车数据处理
     *
     * @param requestVo
     */
    private List<QueryOrderDetailResponseVo> carOrderDataManage(CheckHotelOrderAmountRequest requestVo) {

        //需要扣款的人
        CheckAllOrderAmountRequest checkAllOrderAmountRequest = new CheckAllOrderAmountRequest();
        checkAllOrderAmountRequest.setFlag(requestVo.getFlag());
        Map<Long, List<QueryOrderDetailResponseVo>> carOrderClientLoaderCarOrderAmount = carOrderClientLoader.getCarOrderAmount(checkAllOrderAmountRequest);
        addElkInfoLog("开始处理打车预算逻辑 %s", JsonUtils.toJsonString(carOrderClientLoaderCarOrderAmount));
        List<QueryOrderDetailResponseVo> list = carOrderClientLoaderCarOrderAmount.get(Y);
        if (CollectionUtils.isEmpty(list)) {
            addElkInfoLog("打车不走预算");
            return null;
        }
        //查询预定人成本中心
        List<String> collect = list.stream().map(QueryOrderDetailResponseVo::getOrgId).collect(Collectors.toList());

        calculatedAmount1(list, queryCorpIdByOrgId(collect), "car");
        return null;
    }


    private void calculatedAmount1(List<QueryOrderDetailResponseVo> payBillDataLists, Map<String, String> mapMap, String type) {

        List<GetMbBudgetInfoResponse> response = searchBudgetService.getMbBudgetInfoResponses();
        addElkInfoLog("匹配出得预算中心数据 %s", JsonUtils.toJsonString(response));
        addElkInfoLog("匹配的得公司总数据: %s", JsonUtils.toJsonString(mapMap));
        //Map<String, List<GetMbBudgetInfoResponse>> map = response.stream().collect(Collectors.groupingBy(GetMbBudgetInfoResponse::getCorpId));
        Map<String, List<GetMbBudgetInfoResponse>> map = response.stream().collect(Collectors.groupingBy(GetMbBudgetInfoResponse::getId));
        for (QueryOrderDetailResponseVo payBillDataList : payBillDataLists) {
            String infoId = null;
            String orgName = null;
            Long itemId = null;

            for (GetMbBudgetInfoResponse getMbBudgetInfoResponse : response) {
                if (getMbBudgetInfoResponse.getBudgetType().equals("DEP")) {

                    if (getMbBudgetInfoResponse.getOrgId().contains(payBillDataList.getOrgId())) {
                        infoId = getMbBudgetInfoResponse.getId();
                        orgName = getMbBudgetInfoResponse.getOrgNames().get(payBillDataList.getOrgId());
                        break;
                    }

                } else if (getMbBudgetInfoResponse.getBudgetType().equals("COS")) {

                    if (getMbBudgetInfoResponse.getOrgId().contains(payBillDataList.getCostcenterCode())) {
                        infoId = getMbBudgetInfoResponse.getId();
                        orgName = getMbBudgetInfoResponse.getOrgNames().get(payBillDataList.getCostcenterCode());
                        break;
                    }

                }
            }

            List<GetMbBudgetInfoResponse> mbBudgetInfoResponses = map.get(infoId);
            if (!CollectionUtils.isEmpty(mbBudgetInfoResponses)) {
                for (GetMbBudgetInfoResponse.BudgetInfo budgetInfo : mbBudgetInfoResponses.get(0).getBudgetInfos()) {
                    if (Sets.newHashSet(budgetInfo.getType().split("/")).contains(type)) {
                        itemId = budgetInfo.getId();
                        break;
                    } else {
                        continue;
                    }
                }
            }


            addElkInfoLog("匹配到扣款数据: infoId%s   itemId%s   amount%s", infoId, itemId, payBillDataList.getAmount());

            //update
            //info item  表
            if (!Objects.isNull(infoId) && !Objects.isNull(payBillDataList.getAmount())) {
                addBudgetService.updateAmount(infoId, itemId, payBillDataList.getAmount(), orgName,type);
            } else {
                addElkInfoLog("未匹配到队形得预算");
            }

        }

    }


    public Map<String, String> queryCorpIdByOrgId(List<String> orgIdList) {

        Map<String, String> response = Maps.newHashMap();

        for (String orgId : orgIdList) {
            response.put(orgId, getOrgInfo(orgId).getOrgId());
        }
        return response;
    }

    public MbOrgInfo getOrgInfo(String orgId) {
        Example example = new Example(MbOrgInfo.class);
        example.createCriteria().andEqualTo("orgId", orgId).andEqualTo("deleteTime", "1970-01-01 08:00:00.000");
        MbOrgInfo mbOrgInfo = mbOrgInfoMapper.selectOneByExample(example);
        if (!Objects.equals("C", mbOrgInfo.getOrgType()) && !Objects.equals("G", mbOrgInfo.getOrgType())) {
            mbOrgInfo = getOrgInfo(mbOrgInfo.getParentId());
        }
        return mbOrgInfo;
    }


    public JSONResult<Object> sendEmail () {
        Example example = new Example(BudgetWarningEntity.class);
        example.createCriteria().andGreaterThan("datachangeLasttime", DateUtil.getNowDayYMD()).andEqualTo("isDeleted", 0);

        List<BudgetWarningEntity> budgetWarningEntities = budgetWarningMapper.selectByExample(example);
        for (BudgetWarningEntity budgetWarningEntity : budgetWarningEntities) {
            EmailInfo emailInfo = new EmailInfo();
            emailInfo.setContent(budgetWarningEntity.getContent());
            emailInfo.setReceiveUser(budgetWarningEntity.getEmall());
            emailInfo.setSender_nickName("系统邮件");
            emailInfo.setSubject("预算预警通知");
            sendEmailMQProducer.sendEmail(emailInfo);
        }


        return JSONResult.ok();
    }
}
